﻿import Store from '../../vuex/store';

const util = {
    init({ store }) {
     
    },
    checkAuth(k) {
        if (Store) {
            var _config = Store.getters.getConfiguration;
            var _grantedPolicies = _config.auth.grantedPolicies;

            if (_grantedPolicies[k]) {
                return true;
            }
        }
        return false;
    },
    extend: function() {
        var length = arguments.length;
        var target = arguments[0] || {};
        if (typeof target != "object" && typeof target != "function") {
            target = {};
        }
        if (length == 1) {
            target = this;
            i--;
        }
        for (var i = 1; i < length; i++) {
            var source = arguments[i];
            for (var key in source) {
                // 使用for in会遍历数组所有的可枚举属性，包括原型。
                if (Object.prototype.hasOwnProperty.call(source, key)) {
                    target[key] = source[key];
                }
            }
        }
        return target;
    },
    formatTime: function(v) {
        var date = new Date(parseInt(v.replace("/Date(", "").replace(")/", ""), 10));
        return dateToStr(date);
    },
    formatTimeStamp: function(v) {
        var date = new Date(v * 1000);
        return dateToStr(date);
    },
    formatMoney(v) {
        var num = (v / 100).toFixed(2);
        var parts = num.toString().split(".");
        parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
        return '￥' + parts.join(".");
    }
}

function dateToStr(date) {
    try {
        var _m = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var _d = (date.getDate() < 10 ? "0" : "") + date.getDate();
        var _h = (date.getHours() < 10 ? "0" : "") + date.getHours();
        var _M = (date.getMinutes() < 10 ? "0" : "") + date.getMinutes();
        var _s = (date.getSeconds() < 10 ? "0" : "") + date.getSeconds();
        return date.getFullYear() + "-" + _m + "-" + _d + " " + _h + ":" + _M + ":" + _s;
    }
    catch (err) {
        console.log(err);
        return '-';
    }
}
export default util